﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheepStorage.API.Application.Command.UserInfoCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;
using Lazy.Captcha.Core;
using SheepStorage.Domain.DTO.InputDto;
using SheepStorage.Domain.Enum;
using Serilog;
using SheepStorage.API.Application.Command.PersonnelCommand;
using System.Security.Cryptography;
using System.Text;
namespace SheepStorage.API.Controllers

{
	/// <summary>
	/// 用户模块
	/// </summary>
	[Route("api/[controller]/[action]")]
    [ApiController]
   //  [Authorize]
    public class UserInfoController : ControllerBase
    {
        private readonly IUserInfoService _userService;

        private readonly IMediator mediator;

        private readonly IMapper mapper;
    
        //日志
       

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="mediator"></param>
        /// <param name="mapper"></param>
        public UserInfoController(IUserInfoService userService, IMediator mediator, IMapper mapper)
        {
            _userService = userService;
           this.mediator = mediator;
           this.mapper = mapper;
          

        }
       

        #region 用户模块

     

        
        /// <summary>
        /// 用户登录-Handler
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Results<UserInfoOutput>> UserLoginHandler(UserInfoLoginCommand command)
        {
            command.UserPassWord = this.GetMd5(command.UserPassWord);
            Log.Information($"用户{command.UserName}登录成功");
            return await mediator.Send(command);
            
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetstringMd5(string str)
        {

            var newpwd=  this.GetMd5(str);
            return Ok(newpwd);

        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string GetMd5(string str)
        {
            //创建md5对象
            //创建实例
            var md5 = MD5.Create();
            //调用MD5方法
            byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            //转换
            string newpwd = BitConverter.ToString(hash).Replace("-", "").ToUpper();
            return newpwd;

        }
        /// <summary>
        /// 根据用户ID显示用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<object>> GetUserInfoById(int userId)
        {
            var userInfo = await _userService.GetUserInfoById(userId);
            return Results<object>.DataResult(userInfo);
        }
        /// <summary>
        /// 反填用户基本信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
		public async Task<Results<PersonnelOutput>> GetPersonnelByUserId(int userId)
        {
            var personnel = await _userService.GetPersonnelByUserId(userId);
			return Results<PersonnelOutput>.DataResult(personnel);

		}

		///// <summary>
		///// 根据用户id反填员工基本信息
		///// </summary>
		///// <param name="userId"></param>
		///// <returns></returns>
		//[HttpGet]
		//public Results<UserInfoOutput> GetbaseUserInfoById(int userId)
		//{
		//    var userInfo = _userService.GetbaseUserInfoById(userId);
		//    return Results<UserInfoOutput>.DataResult(userInfo);
		//}

		/// <summary>
		/// 根据用户id修改用户名称
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[HttpPost]
        public async Task< Results<int>> UpdateUserName(UserInfoUpdateInput input)
        {
            var userInfo = await _userService.UpdateUserName(input);
            return Results<int>.DataResult(userInfo);
        }

        /// <summary>
        /// 根据用户id修改用户密码    
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task< Results<int>> UpdateUserPwd(UserInfoUpdatePwdInput input)
        {
            input.UserPassWord = this.GetMd5(input.UserPassWord);
            var userInfo = await _userService.UpdateUserPwd(input);
            return Results<int>.DataResult(userInfo);
        }

        /// <summary>
        /// 随机生成四位验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task< Results<string>> CaptchaImage()
        {
           var code = GetRandomCode();
       
           return Results<string>.DataResult(code);
        }
        


        ////随机生成四位验证码
        private string GetRandomCode()
        {
            string code = "";
            Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                code += random.Next(10);
            }
            return code;
        }
      

        /// <summary>
        /// 显示用户列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<List<UserInfo>>> GetUserInfolist(UserInfoShowCommand command)
        {

            return await mediator.Send(command);
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> AddUserInfoHandler(UserInfoCreateCommand command )
        {
            command.UserPassWord=this.GetMd5(command.UserPassWord);
            return await  mediator.Send(command);
           
        }
        /// <summary>
        /// 增加人员基础信息-已存在信息走修改
        /// </summary>
        /// <param name="personnel"></param>
        /// <returns></returns>
      
        [HttpPost]
        public async Task<Results<int>> AddPersonnelhandler(PersonnelCreateCommand command)
        {
            return await mediator.Send(command);

        }




        /// <summary>
        /// 用户任职信息添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> UpdateStaffInfo(UpdateStaffInput input)
        {
            var res = await   _userService.UpdateStaffInfo(input);
            return Results<int>.DataResult(res);
        }
        /// <summary>
        /// 逻辑删除用户信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> UserInfoDelete(UserInfoDeleteCommand command)
        {
         
            return await mediator.Send(command);

        }
        /// <summary>
        /// 冻结解冻账号   
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="userState">启用状态---1 启用 0 禁用 </param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> UpdateUserState(UserInfoUpdateStateCommand command)
        {

            return await mediator.Send(command);

        }

		/// <summary>
		/// 修改用户头像
		/// </summary>
		/// <param name="userId"></param>
		/// <param name="userState">启用状态---1 启用 0 禁用 </param>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> UpdateUserHeadImage(UserInfoUpdateStateCommand command)
		{

			return await mediator.Send(command);

		}







		#endregion


		#region 用户模块枚举

		/// <summary>
		/// 员工状态(枚举)
		/// </summary>
		/// <returns></returns>
		[HttpGet]
        public IActionResult GetStaffStateEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (StaffState item in Enum.GetValues(typeof(StaffState)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }
        /// <summary>
        /// 身份证件类型 (枚举)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserIdTypeEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (UserIdType item in Enum.GetValues(typeof(UserIdType)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }



        /// <summary>
        /// 学历(枚举)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHighLevelEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (PersonnelHighLevel item in Enum.GetValues(typeof(PersonnelHighLevel)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }
        /// <summary>
        /// 民族枚举
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetNationEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (PersonnelNation item in Enum.GetValues(typeof(PersonnelNation)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }
        /// <summary>
        /// 政治面貌枚举
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPersonnelFaceEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (PersonnelFace item in Enum.GetValues(typeof(PersonnelFace)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }


        /// <summary>
        /// 职务枚举
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPositionEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (Position item in Enum.GetValues(typeof(Position)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }

        /// <summary>
        /// 岗位枚举
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPostStationEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (PostStation item in Enum.GetValues(typeof(PostStation)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }

        /// <summary>
        /// 人事级别枚举
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPersonnelLevelEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (PersonnelLevelEnum item in Enum.GetValues(typeof(PersonnelLevelEnum)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }



        #endregion



    }
}
